package com.example.jmxdemo.jmx;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.ObjectName;

/**
 * Standard MBean由一个接口和一个实现类组成。
 * 接口 : XXXMBean
 * 实现类 : XXX
 * 按照约定，MBean的name为XXX。或自定义。 (实际name=null时报错了...)
 * 如果未按照特定的命名约定进行命名，报错：not implement DynamicMBean, neither follows the Standard MBean conventions
 * <p>
 * attributes:
 * content  (writable)
 * <p>
 * operations:
 * sayHello
 */
@Configuration
public class WelcomeConfig extends NotificationBroadcasterSupport implements InitializingBean, WelcomeConfigMBean {

    @Autowired
    private MBeanServer mbeanserver;

    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public void sayHello() {
        System.out.println("hello........");
//        new Notification("")
//        sendNotification();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ObjectName objectName = new ObjectName("com.example.jmxdemo.jmx:type=WelcomeConfig");
        mbeanserver.registerMBean(this, objectName);
    }

}
